﻿/*
 * Copyright (C) 2015 Karumi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.karumi.dexter;

import com.karumi.dexter.uitool.DexterLogUtil;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.app.AbilityContext;
import ohos.app.Context;
import ohos.bundle.IBundleManager;

/**
 * Wrapper class for all the static calls to the ohos permission system
 */
class HarmonyPermissionService {
    /**
     *  verifyCallingPermission
     * @param context context
     * @param permission permission
     * @return int
     */
    int checkSelfPermission(Context context, String permission) {
        if (context.verifyCallingPermission(permission) == IBundleManager.PERMISSION_GRANTED) {
            return IBundleManager.PERMISSION_GRANTED;
        }
        return IBundleManager.PERMISSION_DENIED;
    }

    /**
     *  Ability#requestPermissionsFromUser
     * @param activity 上下文
     * @param permissions 权限集合
     * @param requestCode 请求code
     */
    void requestPermissions(Ability activity, String[] permissions, int requestCode) {
        if (activity == null) {
            return;
        }

        activity.requestPermissionsFromUser(permissions, requestCode);
    }

    /**
     *  Ability#canRequestPermission
     * @param activity activity
     * @param permission 权限集合
     * @return boolean
     */
    boolean shouldShowRequestPermissionRationale(Ability activity, String permission) {
        if (activity == null) {
            return false;
        }
        DexterLogUtil.info("dexterLogUtil", "25-1");
        return activity.canRequestPermission(permission);
    }

    /**
     * 判断权限是否拒绝
     * @param activity activity
     * @param permission permission
     * @return boolean
     */
    boolean isPermissionPermanentlyDenied(Ability activity, String permission) {
        return !shouldShowRequestPermissionRationale(activity, permission);
    }
}
